package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.DivideConquer;
import cn.pugle.oj.catalog.Unknown;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
 *
 * @author tzp
 * @since 2020/10/19
 */
public class LC34 implements DivideConquer {
    public int[] searchRange(int[] nums, int target) {
        int i = Arrays.binarySearch(nums, 0, nums.length, target);
        if (i < 0) {
            return new int[]{-1, -1};
        } else {
            int left = i, right = i;
            while (left >= 0 && nums[left] == target) left--;
            while (right < nums.length && nums[right] == target) right++;
            return new int[]{left + 1, right - 1};
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new LC34().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8)));
        System.out.println(Arrays.toString(new LC34().searchRange(new int[]{7, 7, 7, 8, 8, 10}, 7)));
    }
}
